package com.shadow.rpc
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.Timer;
	/**
	 * 服务器请求
	 * @author Liseen
	 * 
	 */	

	public class RpcRequest
	{
		private static const STATUS_OK:uint = 0;
		private static const STATUS_BAD:uint = 1;
		private static const SECOND:uint = 1000;
		private static const TIMEOUT:uint = 30 * SECOND;//30000
		
		private var _normalCallback:Function;
		private var _errorCallback:Function;
		private var _urlLoader:URLLoader;
		private var _urlTimer:Timer;
		public function RpcRequest(action:String, params:Object, completCallBack:Function, errorCallBack:Function=null)
		{
			super();
			var dataObj:Object = new Object();
			dataObj.action = action;
			dataObj.params = params;
			this._normalCallback = completCallBack;
			this._errorCallback = (errorCallBack!=null) ? errorCallBack : this.defaultErrorCallback;
			var request:URLRequest = new URLRequest(GlobalContext.flashVars.serverURL);
			request.method = URLRequestMethod.POST;
			var urlVariables:URLVariables = new URLVariables();
			urlVariables.query = JSON.encode(dataObj);
			request.data = urlVariables;
			trace("sends > "+urlVariables+" ( " +typeof(request.data)+" , "+request.method+ " ) "+GlobalContext.flashVars.serverURL);
			this._urlLoader = new URLLoader();
			this._urlLoader.addEventListener(Event.COMPLETE, this.completeHandler);
			this._urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
			this._urlTimer = new Timer(TIMEOUT);
			this._urlTimer.start();
			this._urlTimer.addEventListener(TimerEvent.TIMER, this.onTimeOut);
			this._urlLoader.load(request);
		}
		/**
		 *数据返回 
		 * @param event
		 * 
		 */		
		private function completeHandler(event:Event):void
		{
			trace("received > [ "+ event.currentTarget.data +" ]");
			_urlTimer.stop();
			_urlTimer.removeEventListener(TimerEvent.TIMER,onTimeOut);
			
			if (event.currentTarget.data.charAt(0) != "{")
			{
				this.bridgeErrorCallback(event.currentTarget.data);
				return;
			}
			var responsData:Object = JSON.decode(event.currentTarget.data);
			if (responsData.status == STATUS_OK)
			{
				if ((responsData.data) && (responsData.data.error))
				{
					this._errorCallback(responsData.data);
				}
				else
				{
					RpcClient.instance.rpcResponse(responsData, this._normalCallback);
				}
			}
			else
			{
				if (responsData.status == STATUS_BAD)
				{
					this._errorCallback(responsData.data);
				}
			}
			
		}
		/**
		 *数据报错 
		 * @param value
		 * 
		 */		
		private function bridgeErrorCallback(value:String):void
		{
			trace("Bridge error [ " + value + " ]");
		}
		/**
		 *IOError 
		 * @param event
		 * 
		 */		
		private function onIOError(event:IOErrorEvent):void
		{
			trace("rpcRquest IOError ->"+event.text);
			_urlTimer.stop();
			_urlTimer.removeEventListener(TimerEvent.TIMER,onTimeOut);
			
		}
		/**
		 *请求超时 
		 * @param event
		 * 
		 */		
		private function onTimeOut(event:TimerEvent):void
		{
			trace("rpcRequest TimeOut");
			_urlTimer.stop();
			_urlTimer.removeEventListener(TimerEvent.TIMER,onTimeOut);
			
		}
		
		private function defaultErrorCallback(errorData:Object):void{
			
		}
	}
}